
#include "menubar.h"
#include "ctkPluginContext.h"
#include "service/event/ctkEventConstants.h"
#include <iostream>
#include <QJsonObject>
#include <QJsonDocument>
#include <QAction>

MenuBar::MenuBar(ctkPluginContext *context, QWidget *parent)
        : QMenuBar(parent)
        , context_(context)
{
    menu_descriptions_ << "File" << "Edit" << "MPI" << "Help" << "Page";
    initMenu();
    connect(this, &MenuBar::sig_insertMenu, this, &MenuBar::insertMenuAndActions, Qt::QueuedConnection);
    // 注册监听信号"mainwindow_action"
    ctkDictionary dic;
    dic.insert(ctkEventConstants::EVENT_TOPIC, "mainwindow_action");
    try {
        context_->registerService<ctkEventHandler>(this, dic);
    }
    catch (ctkException e)
    {
        std::cout << e.message().toStdString() << std::endl;
    }
}

MenuBar::~MenuBar()
{

}

void MenuBar::initMenu()
{
    for(const auto &name : menu_descriptions_)
    {
        QMenu *menu = new QMenu(name,this);
        menu_map_[name] = menu;
        addMenu(menu);
    }
}

void MenuBar::handleEvent(const ctkEvent &event)
{
    if(event.getTopic() == "mainwindow_action")
    {
        QStringList names  = event.getPropertyNames();
        for(const auto &name : names)
        {
            QVariant val = event.getProperty(name);
            emit sig_insertMenu(val.toJsonObject());
        }
    }
}

void MenuBar::actionTriggered()
{
    QAction *action = dynamic_cast<QAction *>(sender());
    QString text = action->text();
    std::cout << text.toStdString() << std::endl;
    //获取事件服务接口
    ctkServiceReference ref;
    ctkEventAdmin* eventAdmin{nullptr};
    ref = context_->getServiceReference<ctkEventAdmin>();
    if(ref)
    {
        eventAdmin = context_->getService<ctkEventAdmin>(ref);
        context_->ungetService(ref);
    }
    //发送事件
    ctkDictionary message;
    if(eventAdmin)
        eventAdmin->postEvent(ctkEvent(text, message));
}

void MenuBar::insertMenuAndActions(const QJsonObject &obj)
{
    if(obj.isEmpty())
    {
        return;
    }
    QString key = obj["menu_name"].toString();
    if(menu_map_.contains(key))
    {
        QAction *action = new QAction(obj["action_name"].toString() ,this);
        action->setIcon(QIcon(obj["picture"].toString()));
        connect(action, &QAction::triggered, this, &MenuBar::actionTriggered);
        if(actions_map_.contains(key))
        {
            actions_map_[key].insert(obj["num"].toInt(), action);
        }
        else
        {
            actions_map_.insert(key, QList<QAction*>() << action);
        }
    }
    else
    {
        QMenu *menu = new QMenu(key, this);
        menu_map_.insert(key, menu);

        QAction *action = new QAction(obj["action_name"].toString() ,this);
        action->setIcon(QIcon(obj["picture"].toString()));
        connect(action, &QAction::triggered, this, &MenuBar::actionTriggered);
        actions_map_[key].insert(obj["num"].toInt(), action);

        if(actions_map_.contains(key))
        {
            actions_map_[key].insert(obj["num"].toInt(), action);
        }
        else
        {
            actions_map_.insert(key, QList<QAction*>() << action);
        }
    }
    menu_map_[key]->clear();
    menu_map_[key]->addActions(actions_map_[key]);
}

QMenu *MenuBar::getMenu(const QString &name)
{
    return menu_map_[name];
}

